package cn.cnsy123.bookwebsiteserver.util;

import java.io.IOException;
import java.text.SimpleDateFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

/**
 * Description:json字符串处理工具类<br/>
 * CreateTime:2019-01-04<br/>
 * Creater: yangpengfei@think.cn <br/>
 * @author yangpengfei
 */
public final class JsonUtil {

    private static final Logger logger = LoggerFactory.getLogger(JsonUtil.class);

    /**
     * Description:从字符串转换成Java对象<br/>
     * CreateTime:2019-01-04<br/>
     * Creater: yangpengfei@think.cn <br/>
     * @author yangpengfei
     * @param json
     * @param clazz
     * @return
     */
    public static <T> T jsonStr2Bean(final String json, Class<T> clazz) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        try {
            return objectMapper.readValue(json, clazz);
        } catch (IOException e) {
            StringBuffer errorMessage = new StringBuffer("从json字符串转换成实体类:").append(clazz.getName())
                    .append("失败,json字符串:").append(json).append("失败原因:").append(e.getMessage());
            logger.error(errorMessage.toString(), e);
        }
        return null;
    }

    /**
     * Description:从字符串转换成jsonNode对象<br/>
     * CreateTime:2019-01-04<br/>
     * Creater: yangpengfei@think.cn <br/>
     * @author yangpengfei
     * @param json
     * @return
     */
    public static JsonNode jsonStr2JsonNode(final String json) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        try {
            return objectMapper.readTree(json);
        } catch (IOException e) {
            StringBuffer errorMessage = new StringBuffer("从json字符串转换成JsonNode失败,json字符串:").append(json).append("失败原因:")
                    .append(e.getMessage());
            logger.error(errorMessage.toString(), e);
        }
        return null;
    }

    /**
     * Description:从对象转换成字符串<br/>
     * CreateTime:2019-01-04<br/>
     * Creater: yangpengfei@think.cn <br/>
     * @author yangpengfei
     * @param json
     * @return
     */
    public static String obj2jsonStr(final Object json) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        try {
            return objectMapper.writeValueAsString(json);
        } catch (IOException e) {
            StringBuffer errorMessage = new StringBuffer("从对象转换成json字符串失败,").append("失败原因:").append(e.getMessage());
            logger.error(errorMessage.toString(), e);
        }
        return null;
    }

    /**
     * Description:从对象转换成字符串，其中时间格式自定义<br/>
     * CreateTime:2019-01-04<br/>
     * Creater: yangpengfei@think.cn <br/>
     * @author yangpengfei
     * @param json
     * @param timeParttern
     * @return
     */
    public static String obj2jsonStr(final Object json, String timeParttern) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat(timeParttern));
        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        try {
            return objectMapper.writeValueAsString(json);
        } catch (IOException e) {
            StringBuffer errorMessage = new StringBuffer("从对象转换成json字符串失败,").append("失败原因:").append(e.getMessage());
            logger.error(errorMessage.toString(), e);
        }
        return null;
    }

}
